﻿Namespace CommandBlocks
    Public Class Authenticate
        Public Class Request
            Public PassCode As String
            Public Name As String
            Public Email As String
            Public UniqueId As String
            Public Version As String
        End Class
        Public Class Reply
            Public Success As Boolean
            Public Message As String
            Public PassCode As String
            Public Name As String
            Public Email As String
            Public UniqueId As String
            Public Host As String
            Public Port As Integer
        End Class

        Friend Shared Function HandleRequest(ByVal client As Client, ByVal commandBlock As MessageBlock) As Boolean
            Dim sArgs As String = commandBlock.Message
            Dim clsArgs As Request = Utils.Serialization.FromXml(Of Request)(sArgs)

            Dim clsBud As Entities.Buddy = DataStore.Buddies.GetByUniqueId(clsArgs.UniqueId)
            If clsBud Is Nothing Then
                Manager.OnClientLogEvent(client, String.Format("Buddy with id ""{0}"" does not exist", clsArgs.UniqueId))
                Return False
            End If
            If Not clsBud.PassCode_LoginHere = clsArgs.PassCode Then
                Manager.OnClientLogEvent(client, String.Format("Buddy id ""{0}"" pass code does not match", clsArgs.UniqueId))
                Return False
            End If

            clsBud.LastConnected = Now.ToUniversalTime
            clsBud.Name = clsArgs.Name
            clsBud.Email = clsArgs.Email

            client.Buddy = clsBud

            DataStore.Buddies.Save(clsBud)

            Dim clsReply As New Reply
            clsReply.Success = True
            clsReply.Email = NetNode.Settings.Email
            clsReply.Name = NetNode.Settings.Nickname
            clsReply.PassCode = clsBud.PassCode_LoginAtBuddy
            clsReply.UniqueId = NetNode.Settings.UniqueId
            clsReply.Host = NetNode.Settings.Host
            clsReply.Port = NetNode.Settings.Port

            sArgs = Utils.Serialization.ToXml(clsReply)
            'client.SendCommandBlock("AUTHENTICATEREPLY", sArgs)
            client.PostMessage("AUTHENTICATEREPLY", sArgs)

            Dim strReply As String = client.ReadLine(30)
            If strReply = "AUTHENTICATED" Then
                Manager.OnClientLogEvent(client, "Connection successful")
            Else
                Manager.OnClientLogEvent(client, "Did not succeed in authentication")
            End If
            Return True
        End Function

        Friend Shared Function HandleReply(ByVal client As Client, ByVal commandBlock As MessageBlock) As Boolean
            Dim clsArgs As Reply = Utils.Serialization.FromXml(Of Reply)(commandBlock.Message)

            If Not clsArgs.Success Then
                Manager.OnClientLogEvent(client, "Authenticate failed")
                Return False
            End If

            If client.Buddy.UniqueId <> clsArgs.UniqueId Then
                Manager.OnClientLogEvent(client, "Unique Id did not match!")
                Return False
            End If
            If client.Buddy.PassCode_LoginHere <> clsArgs.PassCode Then
                Manager.OnClientLogEvent(client, "Passcode did not match!")
                Return False
            End If

            client.Buddy.Email = clsArgs.Email
            client.Buddy.Host = clsArgs.Host
            client.Buddy.LastConnected = Now.ToUniversalTime
            client.Buddy.Name = clsArgs.Name
            client.Buddy.Port = clsArgs.Port

            DataStore.Buddies.Save(client.Buddy)

            'send "AUTHENTICATED"
            client.SendLine("AUTHENTICATED")
            Return True
        End Function

    End Class



End Namespace